/**
 * 绝对地址获取接口测试用例
 *
 * Created by raquelmao on 2020/07/10.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const common = require('./../../../common');
const Administrator = require('./../../../../models/administrator');
const uri = require('./../../uri');
const benchmark = require('./../../../../benchmark/analyse-files/self_designed');

module.exports = describe('/GET functions/:functionId/fixed-addresses/:fixedAddressName.json', function () {
  const administrator = new Administrator();
  const newUserIds = [];
  let projectVersion = {};
  let func = {};
  function url(fixedAddressName, functionId) {
    if (!functionId) {
      functionId = func.functionId;
    }
    return `/functions/${functionId}/fixed-addresses/${fixedAddressName}.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((project) => project.createRandomVersion())
      .then((res) => {
        projectVersion = res;
        return projectVersion.getAllFunctions(benchmark);
      })
      .then(([item]) => {
        func = item;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  // 正常获取
  [
    { fixedAddressName: `0x${Math.floor(Math.random() * 100000000).toString(16)}`, length: '10', pointerTargets: { '偏移量 1': { '@type': '', '@length': '19', '@elementType': 'int' } } },
    { fixedAddressName: `0x${Math.floor(Math.random() * 100000000).toString(16)}`, pointerTargets: { '偏移量 1': { '@type': '', '@length': '19', '@elementType': 'int' } } },
    { fixedAddressName: `0x${Math.floor(Math.random() * 100000000).toString(16)}` },
  ].forEach((variable, index) => {
    it(`it should ${index === 0 ? 'GET' : 'get'} fixed address`, function (done) {
      const { fixedAddressName, length, pointerTargets } = variable;
      func.addFixedAddressVariable(fixedAddressName, length, pointerTargets)
        .then(() => {
          // 查询
          chai.request(uri)
            .get(url(fixedAddressName, func.functionId))
            .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.functionFixedAddressMessage(res.body);
              res.body.functionId.should.be.eql(func.functionId);
              res.body.fixedAddressName.should.be.eql(variable.fixedAddressName);
              if (variable.pointerTargets) {
                const { pointerTargets } = variable;
                Object.keys(pointerTargets).forEach((offset) => {
                  res.body.pointerTargets.should.have.property(offset);
                });
              }
              done();
            });
        })
        .catch(done);
    });
  });

  // 不可获取不存在的绝对地址
  const variable = { fixedAddressName: `0x${Math.floor(Math.random() * 100000000).toString(16)}`, length: '2', pointerTargets: { '偏移量 1': { '@type': '', '@length': '19', '@elementType': 'int' } } };
  it('it should fail to get fixed address after deleting it', function (done) {
    new Promise((resolve) => {
      // 查询
      chai.request(uri)
        .get(url(variable.fixedAddressName, func.functionId))
        .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
        .end((err, res) => {
          res.should.have.status(404);
          common.should.be.errorMessage(res.body);
          resolve();
        });
    })
      .then(done)
      .catch(done);
  });
});
